package com.example.flowermall.controller;


import com.example.flowermall.entity.Collect;
import com.example.flowermall.entity.Product;
import com.example.flowermall.entity.Result;
import com.example.flowermall.service.ICollectionService;
import com.example.flowermall.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xiaoman
 * @since 2023-10-24
 */
@RestController
@RequestMapping("/collection")
public class CollectionController {
    @Autowired
    ICollectionService collectionService;
    @Autowired
    IProductService productService;
    @GetMapping("/list/{userId}")
    public Result getCollectionByUserId(@PathVariable("userId")Integer userId){
        List<Collect> byUserId = collectionService.getByUserId(userId);
        List<Product> productList=new ArrayList<>();
        for (int i=0;i<byUserId.size();i++){
            Product product = productService.getGoodsById(byUserId.get(i).getProductId());
            byUserId.get(i).setProduct(product);
            productList.add(product);
        }
        return Result.success(productList);
    }

    @PostMapping("/add")
    public Result addCollection(@RequestBody Collect collect){
        Collect collect1 = collectionService.addCollection(collect);
        return Result.success(collect1);
    }
}
